// ==UserScript==
// @name        eRep Orders
// @version     1
// @description eRepublik Hungarian Orders
// @author      IM
// @namespace   IM
// @include     http://*.erepublik.com/*
// @include     http://erepublik.com/*
// @exclude     http://ads*.erepublik.com/*
// @require     http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js
// @require     http://jquery-json.googlecode.com/files/jquery.json-2.2.min.js
// ==/UserScript==

var orderObj = {};
var thisVersion = 1;
var now = new Date().getTime();
var HOUR = 3600000; // óránként ellenőrizni
//var HOUR = 0; // óránként ellenőrizni

function showRefresh() {
	if (thisVersion != lastVersion) {
		$('#eHUNOrderRefresh').html('<a href="http://ereptoolkit.googlecode.com/svn/order/erep_orders.v'+lastVersion+'.user.js">Telepítsd az új verziót: '+lastVersion +' (Jelenleg:'+thisVersion+')</a>' );
	}
}

function refresh() {
	lastRefresh = GM_getValue('eHunOrderScriptTime',0);
	lastVersion = GM_getValue('eHunOrderScriptVersion',1);
	if (now > lastRefresh+HOUR) {
        GM_xmlhttpRequest({
            method: 'GET',
            url: 'http://ereptoolkit.googlecode.com/svn/order/last?'+now,
            onload:function(responseDetails){
				lastVersion = responseDetails.responseText*1;
				showRefresh();
				GM_setValue('eHunOrderScriptTime',''+now);
				GM_setValue('eHunOrderScriptVersion',''+lastVersion);
			}
		})
	}
	showRefresh();
}

function Main(e) {
		$('#latestnews').before('<div id="eHUNOrder"></div><div id="eHUNOrderRefresh">Version: '+thisVersion+'</div>')
        GM_xmlhttpRequest({
                method: 'GET',
                url: 'http://erepmarket.com/ehungary/parancs/parancs.json?'+now,
                onload:function(responseDetails){
                        var responseText = responseDetails.responseText;
                        orderObj = eval('('+unescape(responseText)+')');
                        $('#eHUNOrder').html('<h3><a href="http://www.erepublik.com/en/battles/show/'
                        +orderObj.battleid+'">'+orderObj.text+'   (' + orderObj.region 
                        + ')</a></h3><h2>Kiadva: '+orderObj.timestamp +' by '+ orderObj.editor
                        +'<br/>Lejár: '+orderObj.endtime +'</h2>');
                }       
        });
        refresh();
}

window.addEventListener('load', function(){var checker=setInterval(function(){
        if(typeof ($ = jQuery.noConflict()) != "undefined") {
                clearInterval(checker);
                Main();
        }
},100);}, false);
